﻿using System;
using System.Collections.Generic;
using System.Linq;
//using System.Security.Claims;
//using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using RunCoachWebApplication.Models;
using RunCoachWebApplication.RunCoachServiceReference;

namespace RunCoachWebApplication.Controllers
{
    public class AccountController : Controller
    {
        RunCoachServiceClient _client = new RunCoachServiceClient();

        public AccountController()
        {
        }

        //
        // GET: /Account/Login
        public ActionResult Login()
        {
            return View();
        }

        //
        // POST: /Account/Login
        [HttpPost]
        public ActionResult Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                int userId = _client.Login(model.UserName, model.Password);
                if (userId > 0)
                {
                    Session["UserName"] = model.UserName;
                    Session["UserId"] = userId;
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Invalid username or password.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Account/Register
        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /Account/Register
        [HttpPost]
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (_client.RegisterUser(model.UserName, model.RealName, model.Password))
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Unsuccessful registration");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Account/Manage
        public ActionResult Manage(ManageMessageId? message)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
                : message == ManageMessageId.Error ? "An error has occurred."
                : "";
            ViewBag.ReturnUrl = Url.Action("Manage");
            return View();
        }

        //
        // POST: /Account/Manage
        [HttpPost]
        public ActionResult Manage(ManageUserViewModel model)
        {
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (ModelState.IsValid)
            {
                String UserName = (String)Session["UserName"];
                if (model.NewPassword.Equals(model.ConfirmPassword) && _client.changePassword(UserName, model.ConfirmPassword, model.OldPassword))
                {
                    return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
                }
                else
                {
                    ModelState.AddModelError("", "Unsuccessful password changing");
                }
            }


            // If we got this far, something failed, redisplay form
            return View(model);
        }

        public ActionResult LogOut()
        {
            Session["UserName"] = null;
            Session["UserId"] = -1;
            _client.Logout();
            return RedirectToAction("Index", "Home");
        }


        #region Helpers

        public enum ManageMessageId
        {
            ChangePasswordSuccess,
            Error
        }

        private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        #endregion
    }
}